package com.symc.his.api.impl.doctor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.symc.his.api.base.BaseApiService;
import com.symc.his.api.base.BaseResponse;
import com.symc.his.api.doctor.WesternMedicineAdvService;
import com.symc.his.api.doctor.dto.req.WesternMedicineAdvInfoReqDTO;
import com.symc.his.api.doctor.dto.req.WesternMedicineAdvReqDTO;
import com.symc.his.api.impl.entity.WesternMedicineAdvDO;
import com.symc.his.api.impl.mapper.MedicalOrderMapper;
import com.symc.his.api.impl.mapper.WesternMedicineAdvMapper;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @CreateTime: 2023/4/30 16:16
 * @Version:
 * @Since: 1.8
 * @Author: 辛凤文 forward.seen@foxmail.com
 */
@RestController
public class WesternMedicineAdvServiceImpl extends BaseApiService implements WesternMedicineAdvService {

    @Resource
    private MedicalOrderMapper medicalOrderMapper;

    @Resource
    private WesternMedicineAdvMapper westernMedicineAdvMapper;

    @Override public String testWesternMedicineAdvService() {
        return "success!!!";
    }

    @Transactional(rollbackFor = SQLException.class)
    @Override public BaseResponse saveWesternMedicineAdvList(WesternMedicineAdvInfoReqDTO medicalAdvInfo) {
        String visitRecordId = medicalAdvInfo.getVisitRecordId();
        List<WesternMedicineAdvReqDTO> westernMedicineAdvList = medicalAdvInfo.getWesternMedicineAdvList();
        LambdaQueryWrapper<WesternMedicineAdvDO> queryWrapper = new LambdaQueryWrapper<WesternMedicineAdvDO>()
            .eq(WesternMedicineAdvDO::getVisitRecordId, visitRecordId);
        List<WesternMedicineAdvDO> existedWesternMedicineList = westernMedicineAdvMapper.selectList(queryWrapper);
        Map<String, WesternMedicineAdvDO> existedWesternMedicineMap = new HashMap<>(3);
        existedWesternMedicineList.forEach(e -> existedWesternMedicineMap.put(e.getDrugCode(), e));
        List<String> reqDrugCodeList = westernMedicineAdvList.stream().map(WesternMedicineAdvReqDTO::getDrugCode).collect(Collectors.toList());
        existedWesternMedicineMap.keySet().forEach(e -> {
            if (reqDrugCodeList.contains(e)) {
                saveWesternMedicineAdv(existedWesternMedicineMap.get(e));
            } else {
                deleteWesternMedicineAdvByVisitRecordIdAndDrugCode(visitRecordId, e);
            }
        });
        return setResultSuccess("保存成功！");
    }

    public BaseResponse saveWesternMedicineAdv(WesternMedicineAdvDO westernMedicineAdv) {
        LambdaQueryWrapper<WesternMedicineAdvDO> queryWrapper = new LambdaQueryWrapper<WesternMedicineAdvDO>()
            .eq(WesternMedicineAdvDO::getVisitRecordId, westernMedicineAdv.getVisitRecordId())
            .eq(WesternMedicineAdvDO::getDrugCode, westernMedicineAdv.getDrugCode());
        Long selectCount = westernMedicineAdvMapper.selectCount(queryWrapper);
        WesternMedicineAdvDO medicineAdvDO = westernMedicineAdv;
        int dmlRes = -1;
        if (selectCount == 0) {
            dmlRes = westernMedicineAdvMapper.insert(medicineAdvDO);
        }
        if (selectCount == 1) {
            dmlRes = westernMedicineAdvMapper.update(medicineAdvDO, queryWrapper);
        }
        if (dmlRes == 1) {
            setResultError("insert or update succeed!");
        }
        if (selectCount > 1) {
            return setResultError("数据库数据出错！");
        }
        return setResultError("insert or update failed!");
    }

    @Override public BaseResponse saveWesternMedicineAdv(WesternMedicineAdvReqDTO westernMedicineAdv) {
        LambdaQueryWrapper<WesternMedicineAdvDO> queryWrapper = new LambdaQueryWrapper<WesternMedicineAdvDO>()
            .eq(WesternMedicineAdvDO::getVisitRecordId, westernMedicineAdv.getVisitRecordId())
            .eq(WesternMedicineAdvDO::getDrugCode, westernMedicineAdv.getDrugCode());
        Long selectCount = westernMedicineAdvMapper.selectCount(queryWrapper);
        WesternMedicineAdvDO medicineAdvDO = DTO2DO(westernMedicineAdv, WesternMedicineAdvDO.class);
        int dmlRes = -1;
        if (selectCount == 0) {
            dmlRes = westernMedicineAdvMapper.insert(medicineAdvDO);
        }
        if (selectCount == 1) {
            dmlRes = westernMedicineAdvMapper.update(medicineAdvDO, queryWrapper);
        }
        if (dmlRes == 1) {
            setResultError("insert or update succeed!");
        }
        if (selectCount > 1) {
            return setResultError("数据库数据出错！");
        }
        return setResultError("insert or update failed!");
    }

    @Override
    public BaseResponse deleteWesternMedicineAdvByVisitRecordIdAndDrugCode(String visitRecordId, String drugCode) {
        LambdaQueryWrapper<WesternMedicineAdvDO> wrapper = new LambdaQueryWrapper<WesternMedicineAdvDO>()
            .eq(WesternMedicineAdvDO::getVisitRecordId, visitRecordId)
            .eq(WesternMedicineAdvDO::getDrugCode, drugCode);
        int deleteRes = westernMedicineAdvMapper.delete(wrapper);
        if (deleteRes == 1) {
            return setResultSuccess("删除成功！");
        }
        return setResultError("删除失败！");
    }

    @Override public BaseResponse queryWesternMedicineAdvListByVisitRecordId(String visitRecordId) {
        LambdaQueryWrapper<WesternMedicineAdvDO> queryWrapper = new LambdaQueryWrapper<WesternMedicineAdvDO>()
            .eq(WesternMedicineAdvDO::getVisitRecordId,visitRecordId);
        List list = westernMedicineAdvMapper.selectList(queryWrapper);
        return setResultSuccess(list);
    }
}